﻿Imports System
Imports System.Collections.ObjectModel
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Workflow.ComponentModel
Imports System.Workflow.ComponentModel.Design

Namespace System.Workflow.Activities
    <ActivityDesignerTheme(GetType(IfElseDesignerTheme))> _
    Friend NotInheritable Class IfElseDesigner
        Inherits ParallelActivityDesigner
        ' Methods
        Public Overrides Function CanInsertActivities(ByVal insertLocation As HitTestInfo, ByVal activitiesToInsert As ReadOnlyCollection([Of] Activity)) As Boolean
            Dim activity As Activity
            For Each activity In activitiesToInsert
                If Not TypeOf activity Is IfElseBranchActivity Then
                    Return False
                End If
            Next
            Return MyBase.CanInsertActivities(insertLocation, activitiesToInsert)
        End Function

        Public Overrides Function CanMoveActivities(ByVal moveLocation As HitTestInfo, ByVal activitiesToMove As ReadOnlyCollection([Of] Activity)) As Boolean
            If ((((Me.ContainedDesigners.Count - activitiesToMove.Count) < 1) AndAlso (Not moveLocation Is Nothing)) AndAlso (Not moveLocation.AssociatedDesigner Is Me)) Then
                Return False
            End If
            Return True
        End Function

        Public Overrides Function CanRemoveActivities(ByVal activitiesToRemove As ReadOnlyCollection([Of] Activity)) As Boolean
            If ((Me.ContainedDesigners.Count - activitiesToRemove.Count) < 1) Then
                Return False
            End If
            Return True
        End Function

        Private Function GetDiamondPath(ByVal rectangle As Rectangle) As GraphicsPath
            Dim points As Point() = New Point() { New Point((rectangle.Left + (rectangle.Width / 2)), rectangle.Top), New Point((rectangle.Right - 1), (rectangle.Top + (rectangle.Height / 2))), New Point((rectangle.Left + (rectangle.Width / 2)), (rectangle.Bottom - 1)), New Point(rectangle.Left, (rectangle.Top + (rectangle.Height / 2))), New Point((rectangle.Left + (rectangle.Width / 2)), rectangle.Top) }
            Dim path As New GraphicsPath
            path.AddLines(points)
            path.CloseFigure
            Return path
        End Function

        Protected Overrides Function OnCreateNewBranch() As CompositeActivity
            Return New IfElseBranchActivity
        End Function

        Protected Overrides Sub OnPaint(ByVal e As ActivityDesignerPaintEventArgs)
            MyBase.OnPaint(e)
            If ((Me.Expanded AndAlso (Me.ContainedDesigners.Count <> 0)) AndAlso (Me Is MyBase.ActiveView.AssociatedDesigner)) Then
                Dim designerTheme As CompositeDesignerTheme = [TryCast](e.DesignerTheme,CompositeDesignerTheme)
                If (Not designerTheme Is Nothing) Then
                    Dim bounds As Rectangle = MyBase.Bounds
                    Dim imageRectangle As Rectangle = Me.ImageRectangle
                    Dim empty As Rectangle = Rectangle.Empty
                    empty.Width = ((designerTheme.ConnectorSize.Height - (2 * e.AmbientTheme.Margin.Height)) + 2)
                    empty.Height = empty.Width
                    empty.X = ((bounds.Left + (bounds.Width / 2)) - (empty.Width / 2))
                    empty.Y = (((bounds.Top + Me.TitleHeight) + ((((designerTheme.ConnectorSize.Height * 3) / 2) - empty.Height) / 2)) + 1)
                    [Using] path As GraphicsPath = Me.GetDiamondPath(empty)
                        e.Graphics.FillPath(designerTheme.ForegroundBrush, path)
                        e.Graphics.DrawPath(designerTheme.ForegroundPen, path)
                    End [Using]
                    empty.Y = (((bounds.Bottom - ((designerTheme.ConnectorSize.Height * 3) / 2)) + ((((designerTheme.ConnectorSize.Height * 3) / 2) - empty.Height) / 2)) + 1)
                    [Using] path2 As GraphicsPath = Me.GetDiamondPath(empty)
                        e.Graphics.FillPath(designerTheme.ForegroundBrush, path2)
                        e.Graphics.DrawPath(designerTheme.ForegroundPen, path2)
                    End [Using]
                End If
            End If
        End Sub

    End Class
End Namespace

